﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
    <h1>请输入xml字符串：</h1>
    <textarea id="xmlArea" rows="6" cols="200" ><p>123</p></textarea>
    <input value="解析xml文档 " onclick="parserXML()" />
    <input value="解析dom文档"  onclick="parserDOM()"/>
    <script type="text/javascript">

        function parserXML() {
            var parser = new DOMParser(),
                strXML = document.getElementById("xmlArea").value,
                parserFlag = true;
            try {
                var xmlDom = parser.parseFromString(strXML, "text/xml");
                var errors = xmlDom.getElementsByTagName("parsererror");
                if (errors.length > 0) {
                    parserFlag = false;
                }
            } catch (e) {
                parserFlag = false;
            }

            if (!parserFlag) {
                alert("解析错误");
            } else {
                alert(xmlDom.documentElement.tagName);
            }

        }

        function parserDOM() {
            var serializer = new XMLSerializer();
            var xml = serializer.serializeToString(document.body);
            alert(xml);
        }

    </script>
</body>
</html>
